/**
 * @file STL_Mutex.cpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 线程锁
 * @version 0.0.1
 * @date 2020-07-16
 * 
 * @coryright Copyright Sci-Tech Laboratories (c) 2020
 * 
 */

#include "STL_Mutex.hpp"

namespace STL
{

    Mutex::Mutex()
    {
        pthread_mutex_init(&m_mtx, nullptr);
    }

    Mutex::~Mutex()
    {
    }

    int Mutex::lock()
    {
        pthread_mutex_lock(&m_mtx);
        return 0;
    }

    int Mutex::unlock()
    {
        pthread_mutex_unlock(&m_mtx);
        return 0;
    }

    int Mutex::trylock()
    {
        return pthread_mutex_trylock(&m_mtx);
    }

    Guard::Guard(STL::Mutex &m) : m_mtx(m)
    {
        m_mtx.lock();
    }

    Guard::~Guard()
    {
        m_mtx.unlock();
    }

} // namespace STL
